package com.example.administrator.yunniuusedcar.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.baidu.platform.comapi.map.D;
import com.example.administrator.common.base.BaseFragment;
import com.example.administrator.common.base.MyApp;
import com.example.administrator.yunniuusedcar.MainActivity;
import com.example.administrator.yunniuusedcar.R;
import com.example.administrator.yunniuusedcar.activity.CommodityDetailsActivity;
import com.example.administrator.yunniuusedcar.adapter.SelectCarAdapter;
import com.example.administrator.yunniuusedcar.adapter.baseadapter.BaseAdapter;
import com.example.administrator.yunniuusedcar.bean.SelectCarData;
import com.example.administrator.yunniuusedcar.model.mode_benn.IHttpService;
import com.example.administrator.yunniuusedcar.refresh.PullRefreshLayout;
import com.example.administrator.yunniuusedcar.util.Judge;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/7.
 *  中规豪车 综合排序
 */

@SuppressLint("ValidFragment")
public class ComprehensivePaiXuFragment extends BaseFragment {
    private int pager = 1;
    private PullRefreshLayout mPull;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView7;
    private  SelectCarAdapter adapter;
    private List<SelectCarData> dataList = new ArrayList<>();
    @SuppressLint("ValidFragment")

    public ComprehensivePaiXuFragment() {
    }

    @Override
    public int getLayoutRes() {
        return R.layout.item_recycleview2;  //fragment_zonghepaixu

    }
    @Override
    public void initView() {
        swipeRefreshLayout = findView(R.id.swipelayout);
        mPull =  findView(R.id.pull);
        initRecycleV();

    }
    private void initRecycleV() {

        recyclerView7 = findView(R.id.recycler_view6);
        LinearLayoutManager linearLayoutManager =new LinearLayoutManager(mActivity);
        recyclerView7.setLayoutManager(linearLayoutManager);
        adapter = new SelectCarAdapter<D>(getContext());
        recyclerView7.setAdapter(adapter);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                dataList.clear();
                pager =1;
                initUloadMenuInfor();
            }
        });


        mPull.setOnPullListener(new PullRefreshLayout.OnPullListener() {
            @Override
            public void onLoadMore(final PullRefreshLayout pullRefreshLayout) {
                pager = pager+1;
                initUloadMenuInfor();
                pullRefreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void initListener() {

        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                int SuiJiCarID =  dataList.get(position).car_id;    //中规豪车 综合排序id

                Intent intent = new Intent(MainActivity.mactivity, CommodityDetailsActivity.class);   // 商品详情
                intent.putExtra("RanCarid",SuiJiCarID);

                MainActivity.mactivity.startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        //手动刷新
        initUloadMenuInfor();
    }
    private void initUloadMenuInfor() {    // 加载数据
//        if(MyApp.enter_type.equals("typeCar")){
//            String ul = IHttpService.URL2 +IHttpService.GETBYCARTYPELIS ;    // 销量排序
//            /** //根据车辆类型选车
//             String GETBYCARTYPELIS="/api/Car/typeCar";*/
//
            String ul2 = IHttpService.HOST_URL + "Car/typeCar";

            OkHttpUtils.post().url(ul2).addParams("sign",IHttpService.SignVal).addParams("page",pager+"").addParams("num","3")
                    .addParams("type","1").addParams("type_id","1").build().execute(new StringCallback() {     //.addParams("type","1")   综合排序

                @Override
                public void onError(Call call, Exception e, int id) {
                }

                @Override
                public void onResponse(String response, int id) {
                    //解析json
                    handlerData(response);
                }
            });
//        }else if(MyApp.enter_type.equals("termCar")){
//            String ul = IHttpService.URL2 +IHttpService.GETSEARCHCARLIST ;

        String ul3 = IHttpService.HOST_URL + "Car/termCar";

            OkHttpUtils.post().url(ul3)
                    .addParams("sign",IHttpService.SignVal)
                    .addParams("page",pager+"")
                    .addParams("num","3")
                    .addParams("type","1")
                    .addParams("type_id", MyApp.type_id+"")
                    .addParams("brand_id",MyApp.brand_id+"")
                    .addParams("level_id",MyApp.level_id+"")
                    .addParams("price_id",MyApp.price_id+"")
                    .build().execute(new StringCallback() {     //.addParams("type","1")   价格排序

                @Override
                public void onError(Call call, Exception e, int id) {
                }

                @Override
                public void onResponse(String response, int id) {
                    handlerData(response);
                }
            });
        }


    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    /**
     * 处理data
     * @param response
     */
    private void handlerData(String response) {
        try {
            swipeRefreshLayout.setRefreshing(false);
            JSONObject jsonObject = new JSONObject(response);
            String data = jsonObject.getString("data");
            dataList .addAll(getDataList(data));
            adapter.refreshData(dataList);
            //recyclerView7.refreshComplete();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private List<SelectCarData> getDataList(String data_str) {
        if (!Judge.isJson(data_str)) {
            return null;
        }

        List<SelectCarData> datas = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(data_str);

            for (int i = 0; i < array.length(); i++) {
                SelectCarData bean = new Gson().fromJson(array.get(i).toString(), SelectCarData.class);
                datas.add(bean);
            }
            return datas;
        } catch (JSONException e) {
            return null;
        }
    }
}
